package com;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import okhttp3.*;

public class PicuiUploader {
    private static final String UPLOAD_URL = "https://picui.cn/api/v1/upload";
    private static final String AUTH_TOKEN = "Bearer 847|M1vyf2l84FjLFRxiwkFZDGAkt4e63irAWt5DvMvz"; // 替换为您的授权 Token

    public static void main(String[] args) throws IOException {
        File imageFile = new File("C:\\Users\\wy\\Desktop\\广州\\大佛古寺1.jpg"); // 替换为您的图片路径
        String response = uploadImage(imageFile);
        System.out.println(response);
    }

    public static String uploadImage(File imageFile) throws IOException {
        OkHttpClient client = new OkHttpClient();
        // 创建请求体
        RequestBody fileBody = RequestBody.create(MediaType.parse(Files.probeContentType(imageFile.toPath())), imageFile);
        MultipartBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", imageFile.getName(), fileBody)
                .build();

        // 创建请求
        Request request = new Request.Builder()
                .url(UPLOAD_URL)
                .addHeader("Authorization", AUTH_TOKEN)
                .addHeader("Accept", "application/json")
                .post(requestBody)
                .build();

        // 执行请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body().string();
        }
    }
}
